package com.dycong.common.common;

import org.apache.commons.lang.SerializationException;

import java.io.*;

/**
 * Created by dycong on 2017/3/15.
 * TODO copy from org.apache.commons.lang SerializationUtils
 */
public class SerializationUtils {

    public SerializationUtils() {
        super();
    }


    public static Object clone(Serializable object) {
        return deserialize(serialize(object));
    }

    public static void serialize(Serializable obj, OutputStream outputStream) {
        if (outputStream == null) {
            throw new IllegalArgumentException("The OutputStream must not be null");
        }
        ObjectOutputStream out = null;
        try {
            // stream closed in the finally
            out = new ObjectOutputStream(outputStream);
            out.writeObject(obj);

        } catch (IOException ex) {
            throw new SerializationException(ex);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }


    public static byte[] serialize(Serializable obj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        serialize(obj, baos);
        return baos.toByteArray();
    }


    public static Object deserialize(InputStream inputStream) {
        if (inputStream == null) {
            throw new IllegalArgumentException("The InputStream must not be null");
        }
        ObjectInputStream in = null;
        try {
            // stream closed in the finally
            in = new ObjectInputStream(inputStream);
            return in.readObject();

        } catch (ClassNotFoundException ex) {
            throw new SerializationException(ex);
        } catch (IOException ex) {
            throw new SerializationException(ex);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }


    public static Object deserialize(byte[] objectData) {
        if (objectData == null) {
            throw new IllegalArgumentException("The byte[] must not be null");
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
        return deserialize(bais);
    }

 /*
 todo my work refer to up
    public class SerializationUtils {

        public SerializationUtils() {
            super();
        }


        public static Object clone(Serializable object) {
            return deserialize(serialize(object));
        }

        public static void serialize(Serializable obj, OutputStream outputStream) {
            if (obj==null|outputStream==null)
                throw new RuntimeException("");
            ObjectOutputStream objectOutputStream=null;
            try {
                objectOutputStream=new ObjectOutputStream( outputStream);
                objectOutputStream.writeObject(obj);

            }catch (IOException e){

            }finally {
                try {
                    objectOutputStream.close();
                }catch (IOException e){

                }
            }
        }


        public static byte[] serialize(Serializable obj) {
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(512);
            serialize(obj,byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        }


        public static Object deserialize(InputStream inputStream) {
            if (null==inputStream)
                throw new RuntimeException("");
            Object o=null;
            try {
                ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
                o=objectInputStream.readObject();

            }catch (IOException e){

            }catch (ClassNotFoundException e){

            }finally {
                try {
                    inputStream.close();
                }catch (IOException e){

                }
            }
            return o;

        }


        public static Object deserialize(byte[] objectData) {
            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(objectData);
            return deserialize(objectData);
        }
    }

    */
}
